<template>
  <div class="login-page">
    <van-nav-bar title="登录"></van-nav-bar>
    <van-form @submit="onSubmit">
      <van-field
        label="用户名"
        name="username"
        placeholder="用户名"
        v-model="username"
        :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: /^\w{6,10}$/, message: '用户名必须6-10' },
        ]"
      ></van-field>
      <van-field
        label="密码"
        type="password"
        name="password"
        placeholder="密码"
        v-model="password"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^\w{6,10}$/, message: '密码必须6-10' },
        ]"
      ></van-field>
      <div style="margin: 16px">
        <van-button type="info" block round>提交</van-button>
      </div>
    </van-form>
    <router-link class="link" to="/register">去注册</router-link>
  </div>
</template>

<script>
// import request from "@/utils/request";
import { login } from "../api/user";
import { setToken } from "../utils/storage";
export default {
  name: "login-page",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    async onSubmit(value) {
      const {
        data: { token },
      } = await login(value);
      setToken(token);
      this.$toast.success("登陆成功");
      this.$router.push("/");
    },
  },
};
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
